package kodkod.analysis;

/*
 * Copies generated files to the desired location using the specified prefix and preserving the suffix.
 * Resolves duplicate names through enumeration.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import edu.mit.csail.sdg.alloy4.A4Reporter;

public class FileReporter extends A4Reporter {
	public File destDir = new File("/tmp"); // destination directory
	public String prefix = "tmp"; // prefix to use when copy
	
	public FileReporter(File dir, String prefix) {
		this.destDir = dir;
		this.prefix = prefix;
	}
	
	public void resultCNF(String filename) {
		String suffix = "." + filename.split("\\.")[1];
		File destFile = new File(destDir, prefix+suffix);
		for(int i = 2; destFile.exists(); i++) {
    		destFile = new File(destDir, prefix+i+suffix);
       	}
		
		try {
    		InputStream src = new FileInputStream(new File(filename));
			OutputStream dest = new FileOutputStream(destFile);
			byte[] buf = new byte[1024];
			int len;
			while ((len = src.read(buf)) > 0) {
				dest.write(buf, 0, len);
			}
			src.close();
			dest.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
